var Resource = {
  resources: {},

  /**
   * Load registered resources.
   * @param {Array<Object>} resources Image and json resources list.
   * @param {Function} allReady Callback function when all resources loaded.
   */
  load: function(resources, allReady) {
    var resourceNum = resources.length;
    var loadedCount = 0;
    var loadImage = this.loadImage;
    var loadJSON = this.loadJSON;
    var that = this;
    var ready = function(id, data) {
      loadedCount++;
      that.resources[id] = data;
      if (resourceNum == loadedCount && allReady)
        allReady(that.resources);
    };

    resources.forEach(function(resource) {
      var image = resource.image;
      var url = image || resource.json;
      var load = image ? loadImage : loadJSON;
      load(url, function(data) {
        ready(resource.id, data);
      });
    });
  },

  loadImage: function(src, callback) {
    var image = new Image();
    image.onload = function() {
      callback(image);
    };
    image.src = src;
  },

  loadJSON: function(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.onload = function() {
      var data = JSON.parse(xhr.responseText);
      callback(data);
    };
    xhr.send(null);
  },

  get: function(id) {
    return this.resources[id];
  }
};